﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Integrated;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Integrated
{
    public class ClientServices : IClientServices
    {

        //异步方法
        public ClientServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }



        /// <summary>
        /// 插入单条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(Client model)
        {
            var result = await Db.InsertAsync(model);
            return result;
        }


        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete Client where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete Client where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }


        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<Client>> GetAllAsync()
        {
            string sql = "select * from Client";
            var list = await Db.QueryAllAsync<Client>(sql);
            return list;
        }


        /// <summary>
        /// 根据条件查询数据  分页
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="ClientCode"></param>
        /// <param name="ClientName"></param>
        /// <param name="Tel"></param>
        /// <returns></returns>
        public async  Task<IEnumerable<Client>> GetAsync(int PageIndex, int PageSize, string ClientCode, string ClientName, string Tel)
        {
            string sql = "select * from Client where 1=1 ";
            if (!string.IsNullOrEmpty(ClientCode))
            {
                sql += " and ClientCode like @ClientCode ";
            }

            if (!string.IsNullOrEmpty(ClientName))
            {
                sql += " and ClientName like @ClientName";
            }

            if (!string.IsNullOrEmpty(Tel))
            {
                sql += " and Tel like @Tel";
            }

            var list = await Db.QueryAsync<Client>(sql, new { PageIndex, PageSize, ClientCode = $"%{ClientCode}%", ClientName = $"%{ClientName}%", Tel = $"%{Tel}%" });
            return list;
        }


        public async Task<int> GetCountAsync(string ClientCode, string ClientName, string Tel)
        {
            string sql = "select * from Client where 1=1 ";
            if (!string.IsNullOrEmpty(ClientCode))
            {
                sql += " and ClientCode like @ClientCode ";
            }

            if (!string.IsNullOrEmpty(ClientName))
            {
                sql += " and ClientName like @ClientName";
            }

            if (!string.IsNullOrEmpty(Tel))
            {
                sql += " and Tel like @Tel";
            }

            var count = await Db.GetRecoreCountAsync(sql, new { ClientCode = $"%{ClientCode}%", ClientName = $"%{ClientName}%", Tel = $"%{Tel}%" });
            return count;
        }

        /// <summary>
        /// 事物修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(Client model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
